<!DOCTYPE html>
<html style="width:100%;height:100%;">
<head lang="en">
    <meta charset="UTF-8">
    <title>管理员列表</title>
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/default/easyui.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/color.css">
    <link type="text/css" rel="stylesheet" href="/resources/easyui/themes/icon.css">
    <link type="text/css" rel="stylesheet" href="/resources/css/yidong.css">
    <script type="text/javascript" src="/resources/easyui/jquery.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.encoding.js"></script>
    <script type="text/javascript" src="/resources/easyui/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/resources/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script type="text/javascript" src="/resources/easyui/plugin/jquery.cookie.js"></script>
    <script type="text/javascript" src="/resources/js/Util.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var pageSize = $.cookie("pageSize");
            if(!pageSize) {
                $.cookie("pageSize", 10, {expires:1});
            }
            loadManagerList($.cookie("pageSize"), 1);
            var pager = $('#managerList').datagrid('getPager');
            if(pager){
                $(pager).pagination({
                    onSelectPage:function(pageNumber, pageSize){
                        loadManagerList(pageSize, pageNumber);
                    },
                    onRefresh:function(pageNumber, pageSize){
                        loadManagerList(pageSize, pageNumber);
                    },
                    onChangePageSize:function(pageSize){
                        $.cookie("pageSize", pageSize, {expires:1});
                        loadManagerList(pageSize, 1);
                    }
                })
            }
        });

        function loadManagerList(pageSize, pageNo, successFun) {
            restClient('/rest/manager/list', function(data){
                $('#managerList').datagrid("loadData", data);
                if(successFun) successFun();
            }, {pageSize:pageSize, pageNo:pageNo});
        }

        function formatGender(value,row,index){
            if(row.gender){
                if(row.gender === 1){
                    return '<span style="color:green">男</span>';
                } else if(row.gender === 2){
                    return '<span style="color:red">女</span>';
                }
            } else {
                return '保密';
            }
        }

        function formatBirthday(value,row,index){
            if(row.birthday){
                return new Date(row.birthday).toLocaleDateString();
            }
        }

        function formatRole(value,row,index){
            if(row.roleId != null){
                var roleName = '';
                restClient('/rest/manager/role/'+row.roleId, function(data){
                    if(data && data[0]) {
                        roleName = data[0].name;
                    }
                }, null, false);
                return roleName;
            }
        }

        function loadRoleCombobox(needSetDefault){
            restClient('/rest/manager/allrole', function(data){
                if(data) {
                    var roleId = $('#roleId');
                    roleId.combobox('loadData', data);
                    if(needSetDefault){
                        roleId.combobox("setValue", data[0].id);
                    }
                }
            }, {roleLevel:$.cookie("roleLevel")});
        }

        function comboFormatBirthday(date){
            var y = date.getFullYear();
            var m = date.getMonth()+1;
            var d = date.getDate();
            return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
        }

        function comboParserBirthday(s){
            if (!s) return new Date();
            if(!isNaN(s)) return new Date(s);
            var ss = (s.split('-'));
            var y = parseInt(ss[0],10);
            var m = parseInt(ss[1],10);
            var d = parseInt(ss[2],10);
            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
                return new Date(y,m-1,d);
            } else {
                return new Date();
            }
        }

        $.extend($.fn.validatebox.defaults.rules, {
            equals: {
                validator: function(value,param){
                    return value == $(param[0]).val();
                },
                message: '两次输入的密码不一致'
            }
        });
    </script>
</head>
<body onload="isLogon();" style="width:100%;height:100%;margin: 0;">
<table id="managerList" class="easyui-datagrid" style="width:100%;height:100%;"
       toolbar="#toolbar" pagination="true"
       data-options="pageList:[10,15,20],pageSize:$.cookie('pageSize') ? $.cookie('pageSize') : 10"
       rownumbers="true" fitColumns="true" singleSelect="true">
    <thead>
    <tr>
        <th field="worknum" width="50">工号</th>
        <th field="name" width="50">登录名</th>
        <th field="alias" width="50">姓名</th>
        <th field="telephone" width="50">电话</th>
        <th field="email" width="50">邮箱</th>
        <th field="roleId" width="50" data-options="formatter:formatRole">角色</th>
        <th field="appId" width="50">APPID</th>
        <!--<th field="cityId" width="50" data-options="formatter:formatCity">城市</th>-->
        <th field="status" width="20" data-options="formatter:formatStatus">状态</th>
    </tr>
    </thead>
</table>
<div id="toolbar">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newOne()">新建</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editOne()">编辑</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="changePsd()">修改密码</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyOne()">删除</a>
</div>

<div id="dlg" class="easyui-dialog" style="width:400px;height:365px;padding:-20px"
     closed="true" buttons="#dlg-buttons">
    <form id="fm" method="post">
        <div class="fitem">
            <label>工号：</label>
            <input id="worknum" name="worknum" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>登录名：</label>
            <input id="name" name="name" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem" id="defaultPsdDiv">
            <label>默认密码：</label>
            <input id="defaultPsd" name="defaultPsd" class="easyui-textbox"
                   data-options="prompt:'123456',disabled:true,editable:false">
        </div>
        <div class="fitem">
            <label>角色：</label>
            <input id="roleId" name="roleId" class="easyui-validatebox easyui-combobox"
                   data-options="valueField:'id', textField:'name', editable:false" required="true">
        </div>
        <div class="fitem">
            <label>APPID：</label>
            <input id="appId" name="appId" class="easyui-validatebox easyui-combobox"
                   data-options="valueField:'appId', textField:'appId', formatter:formatAppId, editable:false" required="true">
        </div>
        <!--<div class="fitem">-->
            <!--<label>城市：</label>-->
            <!--<input id="cityId" name="cityId" class="easyui-combobox easyui-validatebox"-->
                   <!--data-options="valueField:'id', textField:'name', editable:false" required="true">-->
        <!--</div>-->
        <div class="fitem">
            <label>姓名：</label>
            <input id="alias" name="alias" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>电话：</label>
            <input id="telephone" name="telephone" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>邮箱：</label>
            <input id="email" name="email" class="easyui-textbox easyui-validatebox" validType="email"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>状态：</label>
            <select id="status" name="status" class="easyui-combobox easyui-validatebox" data-options="editable:false" required="true">
                <option value="0">启用</option>
                <option value="-1">禁用</option>
                <option value="-2">删除</option>
            </select>
        </div>
    </form>
</div>
<div id="dlg-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="saveOne()" style="width:90px">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')" style="width:90px">取消</a>
</div>

<div id="dlg-psd" class="easyui-dialog" style="width:400px;height:auto;padding:-20px"
     closed="true" buttons="#dlg-psd-buttons">
    <form id="fm-psd" method="post">
        <div class="fitem">
            <label>新密码：</label>
            <input id="psd" name="psd" type="password" class="easyui-textbox easyui-validatebox"
                   required="true" data-options="missingMessage:'该输入项为必输项'">
        </div>
        <div class="fitem">
            <label>确认密码：</label>
            <input id="rePsd" name="rePsd" type="password" class="easyui-textbox easyui-validatebox"
                   required="true" validType="equals['#psd']" data-options="missingMessage:'该输入项为必输项'">
        </div>
    </form>
</div>
<div id="dlg-psd-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton c6" iconCls="icon-ok" onclick="savePsd()" style="width:90px">修改</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg-psd').dialog('close')" style="width:90px">取消</a>
</div>

<script type="text/javascript">
    var url;
    var successMsg;
    function newOne(){
        $('#dlg').dialog('open').dialog('setTitle','新建');
        $('#fm').form('clear');
        loadRoleCombobox(true);
        loadAppIdCombobox($('#appId'), true);
        loadCityCombobox($('#cityId'), true);
        $('#defaultPsdDiv').show();
        $('#gender').combobox("setValue", 0);
        $('#name').textbox('enable');
        $('#status').combobox('setValue', 0);
        url = '/rest/manager/create';
        successMsg = '添加成功';
    }
    function editOne(){
        var row = $('#managerList').datagrid('getSelected');
        if (row){
            $('#dlg').dialog('open').dialog('setTitle', '编辑');
            loadRoleCombobox();
            loadAppIdCombobox($('#appId'));
            loadCityCombobox($('#cityId'));
            $('#defaultPsdDiv').hide();
            $('#fm').form('load', row);
            $('#name').textbox('disable');
            url = '/rest/manager/update/' + row.id;
            successMsg = "编辑成功";
        }
    }
    function saveOne(){
        restClient(url, function (result) {
            if (result.success) {
                $('#dlg').dialog('close');
                loadManagerList($.cookie("pageSize"), 1, function () {
                    $.messager.show({
                        title: '温馨提醒',
                        msg: successMsg
                    });
                });
            } else {
                $.messager.show({
                    title: '温馨提醒',
                    msg: result.msg ? result.msg : result.message
                });
            }
        }, $('#fm').serialize());
    }
    function destroyOne(){
        var row = $('#managerList').datagrid('getSelected');
        if (row){
            $.messager.confirm('确认','确认要删除【'+row.name+'】么?',function(r){
                if (r){
                    restClient('/rest/manager/delete/'+row.id, function(result){
                        if (result.success){
                            loadManagerList($.cookie("pageSize"), 1, function(){
                                $.messager.show({
                                    title: '温馨提醒',
                                    msg: "删除成功"
                                });
                            });
                        } else {
                            $.messager.show({
                                title: '温馨提醒',
                                msg: result.msg ? result.msg : result.message
                            });
                        }
                    });
                }
            });
        }
    }

    var changePsdRow;
    function changePsd() {
        changePsdRow = $('#managerList').datagrid('getSelected');
        if (changePsdRow){
            $.messager.confirm('确认','确认要修改【'+changePsdRow.name+'】的密码么?',function(r){
                if (r){
                    $('#dlg-psd').dialog('open').dialog('setTitle','修改密码');
                    $('#fm-psd').form('clear');
                }
            });
        }
    }

    function savePsd() {
        if(changePsdRow && $('#fm-psd').form('validate')) {
            var psd = $.encoding.digests.hexSha1Str($('#rePsd').textbox("getValue"));
            restClient('/rest/manager/updatePsd/' + changePsdRow.id, function (result) {
                if (result.success) {
                    $('#dlg-psd').dialog('close');
                    loadManagerList($.cookie("pageSize"), 1, function () {
                        $.messager.show({
                            title: '温馨提醒',
                            msg: '修改成功'
                        });
                    });
                } else {
                    $.messager.show({
                        title: '温馨提醒',
                        msg: result.msg ? result.msg : result.message
                    });
                }
            }, {password:psd});
        }
    }
</script>
</body>
</html>